package com.jjvu.news.controller;

import com.jjvu.news.dao.MaxMenu;
import com.jjvu.news.dao.MinMenu;
import com.jjvu.news.service.InfoDetailService;
import com.jjvu.news.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Controller
public class MenuController {
    @Autowired
    private MenuService menuService;
    @RequestMapping("/toMenuManager")
    public String toMenuManager(Model model){
        List<MaxMenu> listMaxmenu =menuService.findMaxMenuAll();
        model.addAttribute("listMaxmenu",listMaxmenu);
        return "admin/MenuManager";
    }
    @RequestMapping("/findMaxMenuById")
    public String findMaxMenuById(Integer max_id,Model model){
        MaxMenu maxMenu=menuService.findMaxMenuById(max_id);
        model.addAttribute("maxMenu",maxMenu);
        return "admin/MaxMenuEdit";
    }
    @RequestMapping("/updateMaxMenu")
    public String updateMaxMenu(MaxMenu maxMenu){
        if (menuService.updateMaxMenu(maxMenu)){
            return "redirect:/toMenuManager";
        }
        return "forward:/findMaxMenuById?max_id="+maxMenu.getMax_id();
    }
    @RequestMapping("/deleteMaxMenu")
    public String deleteMaxMenu(Integer max_id){
        if (menuService.deleteMaxMenu(max_id)){
            return "redirect:/toMenuManager";
        }
        return "forward:/toMenuManager";
    }
    @RequestMapping("/addMaxMenu")
    public String addMaxMenu(MaxMenu maxMenu){
        if (menuService.addMaxMenu(maxMenu)){
            return "redirect:/toMenuManager";
        }
        return "redirect:/toMenuManager";
    }

    @RequestMapping("/findMinMenuByMaxName")
    public String findMinMenuByMaxName(Integer max_id, Model model){
        System.out.println("max_id=========>"+max_id);
        List<MaxMenu> maxMenu=menuService.findMinMenuByMaxName(max_id);
        List<MinMenu> minMenus = null;
        System.out.println(maxMenu);
        try {
            minMenus=maxMenu.get(0).getMinMenus();
        }catch (Exception e){
            e.getMessage();
        }
        System.out.println(minMenus);
        System.out.println(max_id);
        model.addAttribute("listMinMenu",minMenus);
        model.addAttribute("maxid",max_id);
        return "admin/MinManager";
    }

    @RequestMapping("/findMinMenuById")
    public String findMinMenuById(Integer min_id,Model model){
        MinMenu minMenu=menuService.findMinMenuById(min_id);
        List<MaxMenu> maxMenuList=menuService.findMaxMenuAll();
        model.addAttribute("maxMenuList",maxMenuList);
        model.addAttribute("minMenu",minMenu);
        return "admin/MinEdit";
    }
    @RequestMapping("/updateMinMenu")
    public String updateMinMenu(MinMenu minMenu){
        if(menuService.updateMinMenu(minMenu)){
            return "redirect:/findMinMenuByMaxName?max_id="+minMenu.getMaxid();
        }
        return "forward:/findMinMenuById?min_id="+minMenu.getMin_id();
    }
    @RequestMapping("/deleteMinMenu")
    public String deleteMinMenu(Integer min_id,Integer max_id){
        if (menuService.deleteMinMenu(min_id)) {
            return "redirect:/findMinMenuByMaxName?max_id=" + max_id;
        }
        return "forward:/findMinMenuByMaxName?max_id="+max_id;
    }
    @RequestMapping("/addMinMenu")
    public String addMinMenu(MinMenu minMenu){
        if (menuService.addMinMenu(minMenu)){
            return "redirect:/findMinMenuByMaxName?max_id="+minMenu.getMaxid();
        }
        return "forward:/findMinMenuByMaxName?max_id="+minMenu.getMaxid();
    }
}
